//class SettingsPanel
package baidu.reader.txt.ui.component 
{
	import baidu.reader.common.*;
	import baidu.reader.common.model.*;
	import baidu.ui.controls.*;
	import baidu.ui.managers.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class SettingsPanel extends flash.display.Sprite
	{
		[Embed(source="assets/sprite179.swf")]
		private var SettingPanelBG:Class;
		
		private var bg:Sprite;
		
		private var ok_button:baidu.ui.controls.Button;
		
		private var font_combobox:baidu.ui.controls.ComboBox;
		
		private var fontSize_combobox:baidu.ui.controls.ComboBox;
		
		private var leading_combox:baidu.ui.controls.ComboBox;
		
		private var _textFormat:flash.text.TextFormat;
		
		private var prefenrenceModel:baidu.reader.common.model.AppPreferenceModel;
		
		public function SettingsPanel()
		{
			super();
			this.prefenrenceModel = baidu.ui.managers.InstanceManager.createSingletonInstance(baidu.reader.common.model.AppPreferenceModel);
			this.initUI();
			return;
		}
		
		private function initUI():void
		{
			this.filters = [new flash.filters.BlurFilter(0, 0, 0)];
			this.bg = new SettingPanelBG();
			addChild(this.bg);
			this.ok_button = new baidu.ui.controls.Button();
			this.ok_button.useHandCursor = true;
			this.ok_button.setStyle("skin", ResourceFactory.OKButtonSkin);
			addChild(this.ok_button);
			this.ok_button.x = -70;
			this.ok_button.y = 130;
			this.ok_button.addEventListener(flash.events.MouseEvent.CLICK, this.okClickHandler);
			var loc1:*=new baidu.ui.controls.Label();
			loc1.textField.autoSize = flash.text.TextFieldAutoSize.LEFT;
			loc1.text = "文字";
			addChild(loc1);
			loc1.x = -110;
			loc1.y = 30;
			this.font_combobox = new baidu.ui.controls.ComboBox();
			this.font_combobox.addEventListener(flash.events.Event.CHANGE, this.fontChangeHandler);
			this.font_combobox.addItem({"label":"宋体", "value":"宋体,simsun,STSong"});
			this.font_combobox.addItem({"label":"楷体", "value":"楷体,KaiTi,STKaiti"});
			this.font_combobox.addItem({"label":"微软雅黑", "value":"微软雅黑,Microsoft YaHei,黑体,SimHei"});
			this.font_combobox.addItem({"label":"黑体", "value":"黑体,SimHei"});
			addChild(this.font_combobox);
			this.font_combobox.x = -70;
			this.font_combobox.y = 30;
			this.fontSize_combobox = new baidu.ui.controls.ComboBox();
			this.fontSize_combobox.addEventListener(flash.events.Event.CHANGE, this.fontSizeChangeHandler);
			this.fontSize_combobox.addItem({"label":"10号", "value":10});
			this.fontSize_combobox.addItem({"label":"12号", "value":12});
			this.fontSize_combobox.addItem({"label":"14号", "value":14});
			this.fontSize_combobox.addItem({"label":"16号", "value":16});
			this.fontSize_combobox.addItem({"label":"20号", "value":20});
			this.fontSize_combobox.selectedIndex = 2;
			addChild(this.fontSize_combobox);
			this.fontSize_combobox.x = -70;
			this.fontSize_combobox.y = 60;
			var loc2:*=new baidu.ui.controls.Label();
			loc2.textField.autoSize = flash.text.TextFieldAutoSize.LEFT;
			loc2.text = "行距";
			addChild(loc2);
			loc2.x = -110;
			loc2.y = 90;
			this.leading_combox = new baidu.ui.controls.ComboBox();
			this.leading_combox.addEventListener(flash.events.Event.CHANGE, this.leadingChangeHandler);
			this.leading_combox.addItem({"label":"8点", "value":8});
			this.leading_combox.addItem({"label":"12点", "value":12});
			this.leading_combox.addItem({"label":"16点", "value":16});
			addChild(this.leading_combox);
			this.leading_combox.x = -70;
			this.leading_combox.y = 90;
			this.setComboBoxValue(this.font_combobox, this.prefenrenceModel.fontName);
			this.setComboBoxValue(this.fontSize_combobox, this.prefenrenceModel.fontSize);
			this.setComboBoxValue(this.leading_combox, this.prefenrenceModel.leading);
			return;
		}
		
		private function setComboBoxValue(arg1:baidu.ui.controls.ComboBox, arg2:*):void
		{
			var loc1:*=0;
			while (loc1 < arg1.dataProvider.length) 
			{
				if (arg1.dataProvider.getItemAt(loc1).value == arg2)
				{
					arg1.selectedIndex = loc1;
					break;
				}
				++loc1;
			}
			return;
		}
		
		private function calculateFormat():void
		{
			this._textFormat = new flash.text.TextFormat();
			this._textFormat.font = this.font_combobox.selectedItem.value;
			this._textFormat.size = this.fontSize_combobox.selectedItem.value;
			this._textFormat.leading = this.leading_combox.selectedItem.value;
			return;
		}
		
		private function leadingChangeHandler(arg1:flash.events.Event):void
		{
			this.calculateFormat();
			this.prefenrenceModel.leading = int(this._textFormat.leading);
			dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.TEXT_FORMAT_CHANGED, this._textFormat));
			return;
		}
		
		private function fontSizeChangeHandler(arg1:flash.events.Event):void
		{
			this.calculateFormat();
			this.prefenrenceModel.fontSize = int(this._textFormat.size);
			dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.TEXT_FORMAT_CHANGED, this._textFormat));
			return;
		}
		
		private function fontChangeHandler(arg1:flash.events.Event):void
		{
			this.calculateFormat();
			this.prefenrenceModel.fontName = this._textFormat.font;
			dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.TEXT_FORMAT_CHANGED, this._textFormat));
			return;
		}
		
		private function okClickHandler(arg1:flash.events.MouseEvent):void
		{
			dispatchEvent(new baidu.reader.common.ReaderEvent(baidu.reader.common.ReaderEvent.CLOSE_SETTING_PANEL));
			return;
		}
		
		public function get textFormat():flash.text.TextFormat
		{
			return this._textFormat;
		}
		
	
	}
}


